home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / POINTERS.SWG / 0030_Complete Link Lists.pas < prev   
Pascal/Delphi Source File  |  1995-02-28  |  6KB  |  221 lines

  1. (* Program to test link lists. Traverse, add, delete.*)
  2. (* I wrote this program for Pascal 2 to play around with link lists *)
  3. (* It is mostly bullet proof and rather simple, but it works!!! *)
  4. (* I used Pascal 5. BTW, any pointers would be appreciated.     *)
  5. (*                                                 J.C. Wise    *)
  6.  
  7. PROGRAM Link_List (Output, Data);
  8.  
  9. USES
  10.   Crt,Dos,Printer;
  11.  
  12. TYPE
  13.   Line_Str     = String[80];
  14.   Node_Pointer = ^Node_Type;
  15.   Node_Type    = RECORD
  16.                    Component : String;
  17.                    Link      : Node_Pointer
  18.                  END;
  19.  
  20. VAR
  21.   Head,                     (* External pointer to Head *)
  22.   New_Node,                 (* Pointer to the newest node *)
  23.   Current:                  (* Pointer to the last node  *)
  24.      Node_Pointer;
  25.   Data:                     (* File of characters, one per line *)
  26.      Line_Str;
  27.   Line_Num,
  28.   Counter:
  29.      Integer;
  30.   Choice,
  31.   Wait1,
  32.   Item:
  33.      Char;
  34.  
  35.  
  36.  
  37. (**********************************************************************)
  38. PROCEDURE Print_List (VAR Head : Node_pointer);
  39.  
  40.  
  41. BEGIN (* Print_List *)
  42.   CLRSCR;
  43.   Current := Head;
  44.   Line_Num := 1;
  45.   WHILE Current <> NIL DO
  46.      BEGIN
  47.         Write(Line_Num, '. ');
  48.         Line_Num := Line_Num + 1;
  49.         Writeln(Current^.Component);
  50.         Current := Current^.Link;
  51.      END;
  52. END; (* print_list *)
  53.  
  54. (**********************************************************************)
  55. PROCEDURE Printer_List (VAR Head : Node_pointer);
  56.  
  57.  
  58. BEGIN (* Printer_List *)
  59.   CLRSCR;
  60.   Current := Head;
  61.   Line_Num := 1;
  62.   WHILE Current <> NIL DO
  63.      BEGIN
  64.         Write(Lst,Line_Num, '. ');
  65.         Line_Num := Line_Num + 1;
  66.         Writeln(Lst,Current^.Component);
  67.         Current := Current^.Link;
  68.      END;
  69. END; (* Printer_List *)
  70.  
  71.  
  72.  
  73. (**********************************************************************)
  74. PROCEDURE Insrt_List (VAR Head: Node_pointer;
  75.                      Data    : Line_Str );
  76.  
  77. VAR
  78.   Found : Boolean;             (* True when insertion place found *)
  79.   Previous: Node_pointer;      (* Node before current             *)
  80.  
  81. BEGIN (* Insert *)
  82.   New(New_Node);
  83.   New_Node^. Component := Data;
  84.   New_Node^.Link := NIL;
  85.   Previous := NIL;
  86.   Current := Head;
  87.   Found := False;
  88.   Counter := 0;
  89.   WHILE (Current <> NIL) AND NOT Found DO
  90.      BEGIN
  91.         Counter := Counter + 1;
  92.         IF Line_Num > Counter
  93.            THEN
  94.               BEGIN
  95.                  Previous := Current;
  96.                  Current := Current^.Link
  97.               END
  98.            ELSE
  99.               Found := True;
  100.         New_Node^.Link := Current;
  101.      END;
  102.   IF Previous = NIL
  103.      THEN
  104.         Head := New_Node
  105.      ELSE
  106.         Previous^.Link := New_Node;
  107. END; (* Insrt_List *)
  108.  
  109.  
  110. (**********************************************************************)
  111.  
  112. PROCEDURE Delete_List (Line_Num: Integer);
  113.  
  114. VAR
  115.   Current,
  116.   Temp_Pointer:
  117.      Node_pointer;
  118.  
  119. BEGIN (* Delete *)
  120.   Counter := 1;
  121.   IF Line_Num = Counter
  122.      THEN
  123.         BEGIN
  124.            Temp_Pointer := Head;
  125.            Head  := Head^.Link;
  126.            Dispose(Temp_Pointer);
  127.         END
  128.   ELSE
  129.      BEGIN
  130.         Current := Head;
  131.         WHILE (Counter <> Line_Num) AND (Current <> NIL) DO
  132.            BEGIN
  133.               Temp_Pointer := Current;
  134.               Current := Current^.Link;
  135.               Counter := Counter + 1;
  136.            END;(* while *)
  137.         IF (Counter = Line_Num) AND (Current <> NIL)
  138.            THEN
  139.               BEGIN
  140.                  Temp_Pointer^.Link := Current^.Link;
  141.                  Dispose(Current);
  142.               END
  143.         ELSE
  144.            BEGIN
  145.               Writeln('Line # not found');
  146.               Readln(wait1);
  147.               CLRSCR;
  148.            END;
  149.      END;
  150. END; (* delete_list *)
  151.  
  152. (*********************************************************************)
  153.  
  154.  
  155. BEGIN (* Link List *)
  156.   ClrScr;
  157.   Line_Num := 1;
  158.   Head := NIL;
  159.   Writeln('Just start typing!');
  160.   Item := 'A';
  161.   Choice := ' ';
  162.   WHILE UPCASE(Item) <> 'X'  DO
  163.      BEGIN
  164.         CASE UPCASE(Item) of
  165.         'A' :
  166.            BEGIN
  167.               Write(Line_Num, '. ');
  168.               Readln(data);
  169.               WHILE (length(data) <> 0 ) DO
  170.                  BEGIN
  171.                     Insrt_List(Head,data);
  172.                     Line_Num := Line_Num + 1;
  173.                     Write(Line_Num, '. ');
  174.                     Readln(data);
  175.                  END;
  176.            END;
  177.         'D' :
  178.            BEGIN
  179.               Write('Enter the line # to delete ');
  180.               Readln(Line_Num);
  181.               Delete_List(Line_Num);
  182.               Print_List(Head);
  183.            END;
  184.         'I' :
  185.            BEGIN
  186.               Write('Enter the line # to insert before ');
  187.               Readln(Line_Num);
  188.               Write(Line_Num,'. ');
  189.               Readln(Data);
  190.               WHILE (length(data) <> 0 ) DO
  191.                  BEGIN
  192.                     Insrt_List(Head,data);
  193.                     Line_Num := Line_Num + 1;
  194.                     Write(Line_Num, '. ');
  195.                     Readln(data);
  196.                  END;
  197.               Print_List(Head);
  198.            END;
  199.         'P' :
  200.            BEGIN
  201.               Writeln('Send to (P)rinter or (S)creen?');
  202.               Readln(choice);
  203.               CASE UPCASE(choice) OF
  204.                  'P':
  205.                      BEGIN
  206.                         Writeln('Be sure printer is on, enter to continue');
  207.                         Readln(wait1);
  208.                         Printer_List(Head);
  209.                      END ;
  210.                  'S':
  211.                      BEGIN
  212.                         Print_List(Head);
  213.                      END;
  214.               END; (* CASE *)
  215.            END;
  216.         END (* CASE *);
  217.      Writeln('Would you like to (A)dd, (D)elete, (I)nsert, (P)rint or e(X)it? ');
  218.      Readln(Item);
  219.      END;
  220. END.
  221.